package cool.lichuan.config;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.dreamlu.iot.mqtt.codec.MqttPublishMessage;
import net.dreamlu.iot.mqtt.codec.MqttQoS;
import net.dreamlu.iot.mqtt.core.server.event.IMqttMessageListener;
import org.springframework.stereotype.Component;
import org.tio.core.ChannelContext;
import org.tio.utils.buffer.ByteBufferUtil;

import java.nio.charset.StandardCharsets;

/**
 * MQTT服务接收消息
 *
 * @author lichuan
 * @since 2025/6/7 20:02
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class MqttServerMessageListener implements IMqttMessageListener {

    @Override
    public void onMessage(ChannelContext context, String clientId, String topic, MqttQoS qoS, MqttPublishMessage message) {
        String payload = ByteBufferUtil.toString(message.getPayload(), StandardCharsets.UTF_8);
        log.info("Topic：【{}】，收到客户端：【{}】消息：【{}】",  topic, clientId, payload);
    }
}
